<script setup>
import { ref, onMounted } from 'vue'
import {
  getTop20FavoriteStores,
  getFavoriteCountById,
  getReviewCountById,
  getAvgScoreById
} from "@/api/store"
import { getCurrentUserInfo } from "@/api/user";
import { useRouter } from "vue-router";
import axios from "axios";

import foodIcon    from '@/assets/category_icons/1.png'
import drinkIcon   from '@/assets/category_icons/2.png'
import shopIcon    from '@/assets/category_icons/3.png'
import playIcon    from '@/assets/category_icons/4.png'
import hotelIcon   from '@/assets/category_icons/5.png'
import scenicIcon  from '@/assets/category_icons/6.png'
import sportIcon   from '@/assets/category_icons/7.png'
import otherIcon   from '@/assets/category_icons/8.png'


const router = useRouter()

// 获取当前登录用户
const curUserId = ref(0)
const getCurUserId = () => {
  getCurrentUserInfo().then(res => {
    curUserId.value = res.data.id
  })
}

// 主分类
const mainCategories = ref([
  { id: 1, storeName: '美食', icon: foodIcon },
  { id: 2, storeName: '饮品', icon: drinkIcon },
  { id: 3, storeName: '购物', icon: shopIcon },
  { id: 4, storeName: '娱乐', icon: playIcon },
  { id: 5, storeName: '酒店', icon: hotelIcon },
  { id: 6, storeName: '景点', icon: scenicIcon },
  { id: 7, storeName: '运动', icon: sportIcon },
  { id: 8, storeName: '其他', icon: otherIcon }
])
const top20Stores = ref([])

const getTop20Stores = () => {
  getTop20FavoriteStores().then(res => {
    const stores = res.data
    const CountPromises = stores.map(store => {
      return Promise.all([
        getFavoriteCountById(store.id),
        getReviewCountById(store.id),
        getAvgScoreById(store.id)
      ]).then(([favRes, reviewRes, avgScore]) => {
        store.favoriteCount = favRes.data
        store.reviewCount = reviewRes.data
        store.avgScore = avgScore.data
      })
    })

    Promise.all(CountPromises).then(() => {
      top20Stores.value = stores
    })
  })
}

onMounted(() => {
  getCurUserId()
  getTop20Stores()
})

const toStorePage = (id) => {
  router.push(`/user/storeDetailsPage/${id}`)
}

// 搜索功能
const searched = ref(false);
const keyword = ref('');
const stores = ref([]);

const search = async () => {
  if (!keyword.value || !keyword.value.trim()) {
    alert('请输入搜索关键词');
    return;
  }
  try {
    const response = await axios.get('/api/store/search', {
      params: { keyword: keyword.value }
    });
    stores.value = response.data;
    searched.value = stores.value.length === 0;
  } catch (error) {
    console.error('搜索失败：', error);
    alert('搜索出错，请重试！');
  }
};
</script>

<template>
  <div class="home-page">
    <!-- 顶部横幅 -->
    <div class="top-banner">
      <div class="banner-content">
        <h1>发现身边的优质店铺</h1>
        <p>探索本地热门商家，获取真实用户评价</p>
      </div>
      <!-- 装饰元素 -->
      <div class="banner-decoration"></div>
    </div>

    <!-- 主要内容 -->
    <main class="main-content">
      <!-- 搜索区域 -->
      <div class="store-search">
        <div class="search-container">
          <input
              v-model="keyword"
              placeholder="请输入店铺名称或描述"
              class="search-input"
              @keyup.enter="search"
          />
          <button @click="search" class="search-btn">
            <i class="fa fa-search"></i> 搜索
          </button>
        </div>

        <!-- 搜索结果展示 -->
        <div class="result-list" v-if="stores.length > 0">
          <router-link
              class="result-item"
              v-for="(store, index) in stores"
              :key="store.id"
              :to="`/user/storeDetailsPage/${store.id}`"
              :style="`--animation-delay: ${index * 0.05}s`"
          >
            <div class="result-img-container">
              <img :src="store.coverImage" :alt="store.name" class="result-img"/>
            </div>
            <div class="result-info">
              <h3>{{ store.name }}</h3>
              <p>类别：{{ store.category }} > {{ store.sub_category }}</p>
              <p class="result-desc">{{ store.description }}</p>
            </div>
          </router-link>
        </div>
        <div class="no-result" v-else-if="searched">
          <div class="no-result-icon">
            <i class="fa fa-search"></i>
          </div>
          <p>暂无匹配结果</p>
        </div>
      </div>

      <!-- 类别选择 -->
      <div class="categories-section">
        <h2 class="section-title">
          <i class="fa fa-th-large"></i> 分类导航
        </h2>
        <div class="categories-container">
          <router-link
              v-for="(item, index) in mainCategories"
              :key="item.id"
              :to="`/user/category/${item.id}`"
              class="category-item"
              :style="`--animation-delay: ${index * 0.05}s`"
          >
            <div class="category-icon-wrapper">
              <img :src="item.icon" :alt="item.storeName" class="category-icon"/>
            </div>
            <p>{{ item.storeName }}</p>
          </router-link>
        </div>
      </div>

      <!-- 热门店铺 -->
      <div class="hot-stores">
        <h2 class="section-title">
          <i class="fa fa-fire"></i> 热门店铺
        </h2>
        <div class="hot-stores-container">
          <div
              v-for="(store, index) in top20Stores"
              :key="store.id"
              class="store-card"
              @click="toStorePage(store.id)"
              :style="`--animation-delay: ${index * 0.05}s`"
          >
            <div class="store-img-wrapper">
              <img :src="store.coverImage" :alt="store.name" class="store-img"/>
              <div class="store-img-overlay"></div>
            </div>
            <div class="store-info">
              <h3 class="store-name">{{ store.name }}</h3>
              <el-rate
                  v-model="store.avgScore"
                  disabled
                  show-score
                  text-color="#ff9900"
                  score-template="{value} 分"
              />
              <div class="store-tags">
                <span
                    v-for="tag in store.tags"
                    :key="tag"
                    class="store-tag"
                >{{ tag }}</span>
              </div>
              <p class="store-desc">{{ store.description }}</p>
              <div class="store-stats">
                <span class="stat-item">
                  <i class="fa fa-comment-o"></i> {{ store.reviewCount }} 条评论
                </span>
                <span class="stat-item">
                  <i class="fa fa-heart-o"></i> {{ store.favoriteCount }} 人收藏
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>

    <!-- 页脚 -->
    <footer class="page-footer">
      <div class="footer-content">
        <p>© 2023 本地店铺点评平台 版权所有</p>
        <div class="footer-links">
          <a href="#">关于我们</a>
          <a href="#">用户协议</a>
          <a href="#">隐私政策</a>
          <a href="#">联系客服</a>
        </div>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
// 颜色变量 - 增强层次感
$primary: #409eff;
$primary-light: #66b1ff;
$primary-dark: #3a8ee6;
$primary-glass: rgba(64, 158, 255, 0.1);
$primary-glass-dark: rgba(64, 158, 255, 0.05);
$text-primary: #1e293b;
$text-secondary: #64748b;
$text-tertiary: #94a3b8;
$bg-light: #f8fafc;
$bg-white: #ffffff;
$shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
$shadow-hover: 0 8px 30px rgba(0, 0, 0, 0.12);
$transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
$radius: 10px;
$radius-lg: 16px;
$radius-full: 999px;

// 基础样式
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Inter', 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  color: $text-primary;
  background-color: $bg-light;
  line-height: 1.6;
}

.home-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: $bg-light;
}

// 顶部横幅 - 增强视觉层次
.top-banner {
  width: 100%;
  background: linear-gradient(135deg, $primary 0%, $primary-dark 100%);
  color: white;
  padding: 60px 0;
  text-align: center;
  position: relative;
  overflow: hidden;

  // 背景纹理
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: center/cover no-repeat;
    opacity: 0.12;
    z-index: 1;
  }

  .banner-content {
    position: relative;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    z-index: 2;

    h1 {
      font-size: clamp(1.8rem, 4vw, 2.5rem);
      margin-bottom: 18px;
      font-weight: 700;
      text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
      letter-spacing: -0.5px;
    }

    p {
      font-size: clamp(1rem, 2vw, 1.2rem);
      opacity: 0.92;
      max-width: 600px;
      margin: 0 auto;
      text-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
    }
  }

  // 装饰元素
  .banner-decoration {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 30px;
    background: linear-gradient(45deg,
        transparent 33.333%, $bg-light 33.333%,
        $bg-light 66.667%, transparent 66.667%);
    background-size: 30px 30px;
    z-index: 2;
  }
}

.main-content {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  flex: 1;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

// 搜索区域 - 增强交互反馈
.store-search {
  width: 100%;
  margin: -35px auto 45px;
  padding: 24px;
  background-color: $bg-white;
  border-radius: $radius-lg;
  box-shadow: $shadow;
  position: relative;
  z-index: 10;
  transition: transform 0.3s ease, box-shadow 0.3s ease;

  &:hover {
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  }

  .search-container {
    display: flex;
    gap: 0;
    width: 100%;

    .search-input {
      flex: 1;
      padding: 16px 22px;
      border: 2px solid $primary-glass;
      border-right: none;
      border-radius: $radius 0 0 $radius;
      font-size: 16px;
      transition: $transition;

      &:focus {
        outline: none;
        border-color: $primary;
        box-shadow: 0 0 0 4px $primary-glass;
        transform: translateX(2px);
      }

      &::placeholder {
        color: $text-tertiary;
        font-size: 15px;
      }
    }

    .search-btn {
      padding: 0 28px;
      background: linear-gradient(135deg, $primary 0%, $primary-dark 100%);
      color: white;
      border: none;
      border-radius: 0 $radius $radius 0;
      font-size: 16px;
      font-weight: 500;
      cursor: pointer;
      display: flex;
      align-items: center;
      gap: 10px;
      transition: $transition;
      box-shadow: 0 4px 15px rgba(64, 158, 255, 0.2);

      &:hover:not(:active) {
        background: linear-gradient(135deg, $primary-light 0%, $primary 100%);
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(64, 158, 255, 0.3);
      }

      &:active {
        transform: translateY(1px);
        box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
      }
    }
  }

  // 搜索结果优化
  .result-list {
    margin-top: 24px;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    padding-top: 24px;
    border-top: 1px dashed $primary-glass;
  }

  .result-item {
    display: flex;
    gap: 16px;
    padding: 18px;
    background-color: $bg-white;
    border-radius: $radius;
    border: 1px solid $primary-glass;
    transition: $transition;
    text-decoration: none;
    color: inherit;
    opacity: 0;
    animation: fadeIn 0.4s ease-out forwards;
    animation-delay: var(--animation-delay, 0s);

    &:hover {
      transform: translateY(-4px);
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
      border-color: transparent;
    }

    .result-img-container {
      flex-shrink: 0;
      width: 110px;
      height: 110px;
      border-radius: $radius;
      overflow: hidden;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);

      .result-img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.5s ease;
      }

      &:hover .result-img {
        transform: scale(1.08);
      }
    }

    .result-info {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;

      h3 {
        font-size: 17px;
        color: $text-primary;
        margin: 0 0 8px;
        font-weight: 600;
        line-height: 1.4;
      }

      p {
        font-size: 14px;
        color: $text-secondary;
        margin: 4px 0;
        line-height: 1.5;
      }

      .result-desc {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        margin-top: 6px;
      }
    }
  }

  .no-result {
    margin-top: 24px;
    padding: 50px 20px;
    text-align: center;
    color: $text-tertiary;
    font-size: 16px;
    background-color: $primary-glass;
    border-radius: $radius;
    border: 1px dashed $primary-glass-dark;

    .no-result-icon {
      font-size: 48px;
      margin-bottom: 18px;
      color: $primary-light;
      opacity: 0.7;
    }
  }

  @media (max-width: 768px) {
    .result-list {
      grid-template-columns: 1fr;
      gap: 14px;
    }

    .search-container {
      flex-direction: column;
      gap: 12px;

      .search-input {
        border-radius: $radius;
        border-right: 2px solid $primary-glass;
        padding: 14px 20px;
      }

      .search-btn {
        width: 100%;
        border-radius: $radius;
        padding: 14px 20px;
        justify-content: center;
      }
    }
  }
}

// 分类导航 - 增强视觉反馈
.categories-section {
  margin: 0 0 45px;
  background-color: $bg-white;
  padding: 32px 24px;
  border-radius: $radius-lg;
  box-shadow: $shadow;
  position: relative;
  overflow: hidden;

  // 背景装饰
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, $primary, $primary-light);
  }

  .section-title {
    font-size: 20px;
    color: $text-primary;
    margin-bottom: 28px;
    padding-bottom: 12px;
    border-bottom: 1px solid $primary-glass;
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;

    i {
      color: $primary;
      font-size: 22px;
    }
  }

  .categories-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 22px;
  }

  .category-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-decoration: none;
    padding: 22px 10px;
    border-radius: $radius;
    transition: $transition;
    opacity: 0;
    animation: fadeIn 0.4s ease-out forwards;
    animation-delay: var(--animation-delay, 0s);
    position: relative;
    overflow: hidden;

    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 0;
      height: 3px;
      background: linear-gradient(90deg, $primary, $primary-light);
      transition: width 0.3s ease;
    }

    &:hover {
      background-color: $primary-glass;
      transform: translateY(-6px) scale(1.03);
      box-shadow: 0 6px 15px rgba(0, 0, 0, 0.05);

      &::after {
        width: 100%;
      }
    }

    .category-icon-wrapper {
      width: 74px;
      height: 74px;
      border-radius: 50%;
      background-color: $primary-glass;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 14px;
      transition: $transition;
      position: relative;
      z-index: 1;

      &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 0;
        height: 0;
        background-color: rgba(255, 255, 255, 0.3);
        border-radius: 50%;
        transition: width 0.6s ease, height 0.6s ease;
        z-index: -1;
      }

      &:hover::before {
        width: 74px;
        height: 74px;
      }

      .category-icon {
        width: 42px;
        height: 42px;
        object-fit: contain;
        transition: transform 0.3s ease;
      }

      &:hover .category-icon {
        transform: scale(1.15);
      }
    }

    p {
      color: $text-primary;
      font-size: 15px;
      font-weight: 500;
      margin: 0;
      text-align: center;
      letter-spacing: 0.2px;
    }
  }
}

// 热门店铺 - 增强卡片质感
.hot-stores {
  margin-bottom: 65px;

  .section-title {
    font-size: 20px;
    color: $text-primary;
    margin-bottom: 28px;
    padding-bottom: 12px;
    border-bottom: 1px solid $primary-glass;
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;

    i {
      color: #ff7a45;
      font-size: 22px;
    }
  }

  .hot-stores-container {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 28px;
  }

  .store-card {
    display: flex;
    flex-direction: column;
    background: $bg-white;
    border-radius: $radius-lg;
    overflow: hidden;
    box-shadow: $shadow;
    transition: $transition;
    cursor: pointer;
    height: 100%;
    opacity: 0;
    animation: fadeIn 0.5s ease-out forwards;
    animation-delay: var(--animation-delay, 0s);

    &:hover {
      transform: translateY(-6px);
      box-shadow: $shadow-hover;
    }

    .store-img-wrapper {
      position: relative;
      overflow: hidden;
      height: 200px;
    }

    .store-img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.7s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    }

    &:hover .store-img {
      transform: scale(1.08);
    }

    .store-img-overlay {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(transparent 60%, rgba(0, 0, 0, 0.3) 100%);
      z-index: 1;
      opacity: 0;
      transition: opacity 0.3s ease;
    }

    &:hover .store-img-overlay {
      opacity: 1;
    }

    .store-info {
      flex: 1;
      padding: 24px;
      display: flex;
      flex-direction: column;

      .store-name {
        font-size: 18px;
        font-weight: 600;
        color: $text-primary;
        margin: 0 0 12px;
        display: -webkit-box;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
        overflow: hidden;
        line-height: 1.4;
      }

      .el-rate {
        margin-bottom: 10px;
      }

      .store-tags {
        margin: 12px 0;
        display: flex;
        flex-wrap: wrap;
        gap: 8px;

        .store-tag {
          display: inline-block;
          background: $primary-glass;
          color: $primary;
          font-size: 12px;
          padding: 3px 10px;
          border-radius: 4px;
          transition: $transition;

          &:hover {
            background: $primary;
            color: white;
          }
        }
      }

      .store-desc {
        font-size: 14px;
        color: $text-secondary;
        margin: 0 0 18px;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        line-height: 1.6;
        flex: 1;
      }

      .store-stats {
        display: flex;
        justify-content: space-between;
        font-size: 14px;
        color: $text-secondary;
        margin-top: 12px;
        padding-top: 16px;
        border-top: 1px solid $primary-glass;

        .stat-item {
          display: flex;
          align-items: center;
          gap: 6px;
          transition: color 0.3s ease;

          i {
            font-size: 15px;
          }

          &:hover {
            color: $primary;
          }
        }
      }
    }
  }

  @media (max-width: 768px) {
    .hot-stores-container {
      grid-template-columns: 1fr;
      gap: 22px;
    }

    .store-img-wrapper {
      height: 180px;
    }

    .store-info {
      padding: 20px;
    }
  }
}

// 页脚 - 增强交互体验
.page-footer {
  background-color: $text-primary;
  color: $text-tertiary;
  padding: 50px 20px 25px;
  margin-top: 70px;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, $primary, $primary-light);
  }

  .footer-content {
    max-width: 1200px;
    margin: 0 auto;
    text-align: center;

    p {
      margin-bottom: 25px;
      font-size: 14px;
      opacity: 0.85;
    }

    .footer-links {
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      gap: 12px 24px;
      margin-bottom: 35px;

      a {
        color: $text-tertiary;
        text-decoration: none;
        font-size: 14px;
        transition: $transition;
        position: relative;
        padding: 3px 0;

        &::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 0;
          width: 0;
          height: 1px;
          background-color: $primary;
          transition: width 0.3s ease;
        }

        &:hover {
          color: white;

          &::after {
            width: 100%;
          }
        }
      }
    }
  }
}

// 动画效果
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(15px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 滚动动画触发
@keyframes fadeInOnScroll {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 为延迟加载元素添加动画类
.categories-container, .hot-stores-container, .result-list {
  opacity: 0;
  animation: fadeInOnScroll 0.8s ease-out forwards;
  animation-delay: 0.1s;
}
</style>